﻿using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebConfigModifier.Samples;

namespace WebConfigModifier.Tests
{
    public class ReplaceConnectionStringTests
    {
        [TestClass]
        public class WhenReplacingAnExistingConnectionString
        {
            private string _connectionStringXPath;
            private string _connectionStringReplacementValue;

            private XDocument _doc;
            private DocumentModifier _modifier;
            private XElement _modifiedElement;

            [TestInitialize]
            public void Setup()
            {
                _connectionStringXPath = "/configuration/connectionStrings/add[@name='ApplicationServices']";
                _connectionStringReplacementValue = "ReplacedConnectionString";

                _doc = XDocument.Parse(Resources.Mvc2_Vanilla_Web);
                _modifier = new DocumentModifier(_doc);
                _modifier.ReplaceAttributeValue(_connectionStringXPath, "connectionString", _connectionStringReplacementValue);

                // find the element within the document using a different traversal method (LINQ rather than XPath) to sanity-check.
                _modifiedElement = _doc.Elements("configuration").Elements("connectionStrings").Elements("add").First();
            }

            [TestMethod]
            public void TheConnectionStringShouldBeUpdated()
            {
                Assert.AreEqual(_connectionStringReplacementValue, _modifiedElement.Attribute("connectionString").Value);
            }
        }
    }
}
